/*  
 *  Copyright (c) 2008 Adrian Necimal <necimal@gmail.com>
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Make a namespace.
if (typeof LastfmImages == 'undefined') {
  var LastfmImages = { };
}

LastfmImages.ImageService = {
  findArtwork: function(artist, album) {
    // try looking for an album cover
    if (album && this.findAlbumArt(artist, album)) {
      return;
    };
    
    // then try looking for an artist pic
    this.findArtistArt(artist);
  },
  
  display: function(uri, text) {
    if (!this._imgbox) {
      this._imgbox = document.getElementById("lastfm-albumart-imgbox");
    }
    
    this._imgbox.src = uri;
    this._imgbox.tooltipText = text;
  },

  clear: function() {
    this.display("chrome://lastfm-albumart/skin/default.png", null);
  },
  
  formatURL: function(url) {
    if (!url) return "";
    
    // we need to escape ampersands
    else return url.replace("&", "%2526");
  },
  
  findAlbumArt: function(artist, album) {
    var url = "http://ws.audioscrobbler.com/1.0/album/" +
              artist + "/" + album + "/info.xml";
    url = this.formatURL(url);
    
    var req = new XMLHttpRequest();
    req.open("GET", url, false);
    req.send(null);
    
    if (!req.responseXML) { return false; }
    
    var albumnode = req.responseXML.getElementsByTagName("album").item(0);
    var artistname = albumnode.getAttribute("artist");
    var albumtitle = albumnode.getAttribute("title");

    var artnode = req.responseXML.getElementsByTagName("coverart").item(0);
    var picture = artnode.getElementsByTagName("medium").item(0).firstChild.nodeValue;
    
    if (picture) {
      var text = artistname + " - " + albumtitle;
      this.display(picture, text);
      return true;
    } else {
      return false;
    }
  },
  
  findArtistArt: function(artist) {
    var url = "http://ws.audioscrobbler.com/1.0/artist/" +
              artist + "/similar.xml";
    url = this.formatURL(url);
    
    var req = new XMLHttpRequest();
    req.open("GET", url, false);
    req.send(null);
    
    if (!req.responseXML) { return false; }
    
    var similarnode = req.responseXML.getElementsByTagName("similarartists").item(0);
    var artistname = similarnode.getAttribute("artist");
    var picture = similarnode.getAttribute("picture");
    
    if (picture) {
      this.display(picture, artistname);
      return true;
    } else {
      return false;
    }
  },
  
  onLoad: function(e) {
    LastfmImages.PlayObserver.init();
  },
  
  onUnload: function(e) {
    LastfmImages.PlayObserver.tidyup();
  },
};

LastfmImages.PlayObserver = {
  init: function() {
    this._pps = Components.classes["@songbirdnest.com/Songbird/PlaylistPlayback;1"]
                  .getService(Components.interfaces.sbIPlaylistPlayback);
    this._pps.addListener(this);
    this.onStop();
  },
  
  tidyup: function() {
    this._pps.removeListener(this);
  },

  onBeforeTrackChange: function() { },
  
  onTrackChange: function() {
    // get some info
    var mediaItem = this._pps.playingView.mediaList.getItemByGuid(this._pps.currentGUID);
    
    var artist = mediaItem.getProperty("http://songbirdnest.com/data/1.0#artistName");
    var album = mediaItem.getProperty("http://songbirdnest.com/data/1.0#albumName");
    
    LastfmImages.ImageService.findArtwork(artist, album);
  },
  
  onStop: function() {
    LastfmImages.ImageService.clear();
  }
};

window.addEventListener("load", LastfmImages.ImageService.onLoad, false);
window.addEventListener("unload", LastfmImages.ImageService.onUnload, false);
